package xiaoyang.controller.back;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import xiaoyang.pojo.AppInfo;
import xiaoyang.pojo.AppVersion;
import xiaoyang.pojo.DataDictionary;
import xiaoyang.service.AppCategoryService;
import xiaoyang.service.AppInfoService;
import xiaoyang.service.DataDictionaryService;

import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/backappInfo")
public class BackAppInfoController {
    @Resource
    private AppInfoService appInfoService;
    @Resource
    private AppCategoryService appCategoryService;
    @Resource
    private DataDictionaryService dataDictionaryService;
    @RequestMapping("/appInfoList")
    public Object appInfoList(@RequestParam(required = false,defaultValue = "")String softwareName,
                              @RequestParam(required = false,defaultValue = "0") String flatformId,
                              @RequestParam(required = false,defaultValue = "0") String categoryLevel1,
                              @RequestParam(required = false,defaultValue = "0") String categoryLevel2,
                              @RequestParam(required = false,defaultValue = "0") String categoryLevel3,
                              @RequestParam(required = false,defaultValue = "1")Integer pageNum,
                              @RequestParam(required = false,defaultValue = "5")Integer pageSize, Model model){
        if (pageNum<0){
            pageNum=1;
        }
        int totalCount=appInfoService.appInfoCount(softwareName,Integer.parseInt(flatformId),Integer.parseInt(categoryLevel1),Integer.parseInt(categoryLevel2),Integer.parseInt(categoryLevel3));
        int totalPage=totalCount%pageSize==0?(totalCount/pageSize):(totalCount/pageSize+1);
        List<AppInfo> appInfoList=appInfoService.getInfoAll(softwareName,Integer.parseInt(flatformId),Integer.parseInt(categoryLevel1),Integer.parseInt(categoryLevel2),Integer.parseInt(categoryLevel3),(pageNum-1)*pageSize,pageSize);
        List<DataDictionary> dictionary = dataDictionaryService.getDictionary();
        model.addAttribute("ddry", dictionary);
        model.addAttribute("appInfo", appInfoList);
        model.addAttribute("total",totalPage);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("pageIndex",pageNum);
        model.addAttribute("softwareName",softwareName);
        model.addAttribute("categoryLevel1",categoryLevel1);
        model.addAttribute("categoryLevel2",categoryLevel2);
        model.addAttribute("categoryLevel3",categoryLevel3);
        model.addAttribute("flatformId",flatformId);
        return "appInfoList";
    }
    @RequestMapping("/auditView/{id}/{versionId}")
    public Object auditView(@PathVariable Integer id, @PathVariable Integer versionId, Model model){
        AppInfo idAll = appInfoService.getAppInfoId(id);
        AppVersion appVersion = appInfoService.appVersionId(versionId);
        model.addAttribute("appView",idAll);
        model.addAttribute("version",appVersion);
        return "audit";
    }
    @RequestMapping("/auditPass/{id}/{version}")
    public Object auditPass(@PathVariable Integer id,@PathVariable Integer version){
        if (appInfoService.auditPass(id,version)){
            return "redirect:/backappInfo/appInfoList";
        }else {
            return "error";
        }

    }
    @RequestMapping("/auditNoPass/{id}/{version}")
    public Object auditNoPass(@PathVariable Integer id,@PathVariable Integer version){
        if (appInfoService.auditNoPass(id,version)){
            return "redirect:/backappInfo/appInfoList";
        }else {
            return "error";
        }

    }
}
